// CxEdit.cpp : implementation file
//

#include "stdafx.h"
#include "comtest.h"
#include "CxEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCxEdit

CCxEdit::CCxEdit()
{
}

CCxEdit::~CCxEdit()
{
}


BEGIN_MESSAGE_MAP(CCxEdit, CEdit)
	//{{AFX_MSG_MAP(CCxEdit)
		ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()




BOOL CCxEdit::PreTranslateMessage(MSG *lpMsg)
{
	int   nStart,nEnd;

	if(m_IsOx)
	{
		if(lpMsg->message==WM_KEYDOWN)
		{
			if(lpMsg->wParam==VK_BACK)
			{
				
				GetSel(nStart,nEnd);
				if(nStart!=nEnd)return TRUE;
				if(m_IsEven)
				{
					m_IsEven=!m_IsEven;
					CEdit::PreTranslateInput(lpMsg);
					return CEdit::PreTranslateInput(lpMsg);
				}
				else 
				{
					m_IsEven=!m_IsEven;
					return CEdit::PreTranslateInput(lpMsg);
				}
			}
		}
	}

	return CEdit::PreTranslateMessage(lpMsg);
 }



void CCxEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if(m_IsOx)
	{
		if(nChar == ' ')
		{
			if(m_SpaceLock)
			{
				m_SpaceLock=FALSE;
				CEdit::OnChar(nChar,nRepCnt,nFlags);
			}
			else return ;
		}
		else if(	(nChar > 'F' && nChar <= 'Z')	||
			(nChar > 'f' && nChar <= 'z'))return ;
		else if(	(nChar <= 'F' && nChar >= 'A')  ||
					(nChar <= 'f' && nChar >= 'a')  ||
					(nChar <= '9' && nChar >= '0')
				)
		{
			CEdit::OnChar(nChar,nRepCnt,nFlags);
			m_IsEven=!m_IsEven;
			if(m_IsEven)
			{
				m_SpaceLock=TRUE;
				SendMessage(WM_CHAR,(WPARAM)(0x00000020),(LPARAM)(0x00200001));
			}
		}
		else CEdit::OnChar(nChar,nRepCnt,nFlags);
	}

	else CEdit::OnChar(nChar,nRepCnt,nFlags);
}



void  CCxEdit::GetTransedText(CString &str)
{
	if(!m_IsOx)
	{
		GetWindowText(str);
		return ;
	}

	CString  buffer;

	GetWindowText(buffer);
	int		 i;
	size_t   wordsize=0;
	size_t   totallength=buffer.GetLength();

	if(totallength <= 3)wordsize=1;	
	else 
	{
		if( (totallength-2) % 3 == 0)
			wordsize = ( totallength+1) / 3;
		else if((totallength-2) % 3 == 1)
			wordsize = totallength / 3;
		else  
		{
			wordsize = ( totallength+2) / 3;			
		}
	}

	buffer+="0 ";

	BYTE	*pTemp=new BYTE[wordsize+1];

	BYTE	  ch;
	BYTE      _ch[2];

	for(i=0;i<wordsize;++i)
	{	
	
		ch=buffer[i*3+0];
		if( ch <= 'F' && ch >= 'A')_ch[0]=(ch-'A')+10;
		else if( ch <= 'f' && ch >= 'a')_ch[0]=(ch-'a')+10;
		else _ch[0]=(ch-'0');

	
		ch=buffer[i*3+1];
		if( ch <= 'F' && ch >= 'A')_ch[1]=(ch-'A')+10;
		else if( ch <= 'f' && ch >= 'a')_ch[1]=(ch-'a')+10;
		else _ch[1]=(ch-'0');
		
		ch = ( _ch[0] << 4 )	+	_ch[1];	

		pTemp[i]=ch;
	}
	pTemp[wordsize]='\0';

	str=CString(reinterpret_cast<char *>(pTemp));
}

